
var prefix = "/userAunt";
$(function() {
    layui.use(['table', 'dropdown','form','laydate'], function(){
        var table = layui.table,
            form = layui.form ,
            laydate = layui.laydate,
            $ = layui.$;
        // const mySclect=(url='/userAunt/seleProject')=>{
        //     $('#mySelect-projectId').attr("disabled", true);
        //     AjaxGet(url).then(function (response) {
        //         myDataList = response.data
        //         //设置select
        //         $('#mySelect-projectId').empty();
        //         $('#mySelect-projectId').append($('<option>', {
        //             value: '',
        //             text: '请选择服务类型'
        //         }))
        //         response.data.forEach(item => {
        //             $('#mySelect-projectId').append($('<option>', {
        //                 value: item.id,
        //                 text: item.name,
        //                 selected: item.id == $('#projectId').val()
        //             }))
        //         })
        //         $('#mySelect-projectId').attr("disabled", $('#type').val() == 'transferor'||$('#type').val() == 'edit');
        //         form.render('select');
        //     })
        // }
        // mySclect()
        laydate.render({
            elem: '#ID-laydate-range',
            range: ['#createTime', '#endTime'],
            rangeLinked: true,
            type: 'datetime',
            shortcuts: [
                {
                    text: "上个月",
                    value: function () {
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth();
                        return [
                            new Date(year, month - 1, 1),
                            new Date(year, month, 0)
                        ];
                    }
                }, {
                    text: "这个月",
                    value: function () {
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth();
                        return [
                            new Date(year, month, 1),
                            new Date(year, month + 1, 0)
                        ];
                    }
                },
                {
                    text: "下个月",
                    value: function () {
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth();
                        return [
                            new Date(year, month + 1, 1),
                            new Date(year, month + 2, 0)
                        ];
                    }
                }
            ],
            done: function(value, date, endDate){
                let mydataList=value.split(' - ')
                $('#createTime').val(mydataList[0])
                $('#endTime').val(mydataList[1])
            }
        });
        laydate.render({
            elem: '#ID-laydate-range1',
            range: ['#createTime1', '#endTime1'],
            rangeLinked: true,
            type: 'datetime',
            // min: $('#endTime').val(),
            shortcuts: [
                {
                    text: "上个月",
                    value: function () {
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth();
                        return [
                            new Date(year, month - 1, 1),
                            new Date(year, month, 0)
                        ];
                    }
                }, {
                    text: "这个月",
                    value: function () {
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth();
                        return [
                            new Date(year, month, 1),
                            new Date(year, month + 1, 0)
                        ];
                    }
                },
                {
                    text: "下个月",
                    value: function () {
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth();
                        return [
                            new Date(year, month + 1, 1),
                            new Date(year, month + 2, 0)
                        ];
                    }
                }
            ],
            done: function(value, date, endDate){
                let mydataList=value.split(' - ')
                $('#createTime1').val(mydataList[0])
                $('#endTime1').val(mydataList[1])
            }
        });
        // 创建渲染实例
        table.render({
            elem: '#exampleTable'
            ,id:'exampleTable'
            ,method:"POST"
            ,contentType: 'application/json'
            ,url: "/userHouse/getUserHousePage"
            ,cellMinWidth: 80
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                , curr: pages //设定初始在第 5 页
                , groups: 5 //只显示 1 个连续页码
                , limit : 15
                , limits : [15,30,50,100,200]
            }
            ,request:{
                pageName: 'curr',
                limitName: 'pageSize'
            }
            ,where: {
                username:$("#username").val(),
                auditStatus:parseInt($("#auditStatus").val()),
                mobile:$("#phone").val(),
                status:$('#myStatus').val(),
                // projectId: $("#mySelect-projectId").val(),
                communityName: $("#communityName").val(),
                movieHouseTime: $("#createTime").val(),
                movieHouseTime1: $("#endTime").val(),
                leaveTime: $("#createTime1").val(),
                leaveTime1: $("#endTime1").val(),
            }
            ,defaultToolbar: false
            ,loading: false
            ,cols: [[
                {
                    field : 'username',
                    title : '申请人',
                    align : 'center'
                },
                {
                    field : 'phone',
                    title : '联系方式',
                    align : 'center'
                },
                {
                    field : 'movieHouseTime',
                    title : '入住时间',
                    align : 'center',
                    templet:function (d) {
                        return `<span>${d.movieHouseTime1}</span>`
                    }
                },
                {
                    field : 'stayDays',
                    title : '入住天数',
                    align : 'center'
                },
                {
                    field : 'leaveTime',
                    title : '离开时间',
                    align : 'center',
                    templet:function (d) {
                        return `<span>${d.leaveTime}</span>`
                    }
                },
                {
                    field : 'communityName',
                    title : '小区名称',
                    align : 'center'
                },
                {
                    field : 'buildNumber',
                    title : '楼栋房号',
                    align : 'center'
                },
                {
                    field : 'bedCode',
                    title : '床号',
                    align : 'center'
                },
                {
                    field : 'status',
                    title : '入住状态',
                    align : 'center',
                    templet:function (d) {

                        return d.status==0?
                        '<span class="label label-primary">待入住</span>':
                        d.status==1?
                            '<span class="label label-warning">已入住</span>':
                        d.status==2?
                            '<span class="label" style="background-color: #e9edf1;color: #a1a4ac">已离开</span>':
                            '<span class="label label-warning">-</span>'

                    }
                },
                // {
                //     field: 'projectId',
                //     title: '服务类型',
                //     align: 'center',
                //     templet: function (d) {
                //         let str = '';
                //         let projectName = d.projectName;
                //         if ( projectName != null ){
                //             projectName = projectName.split('、');
                //             projectName.map(function (v,k){
                //                 if( v != '' ){
                //                     if ( k <= 2) {
                //                         str += v+'、';
                //                     }
                //                 }
                //             })
                //             str=str.substring(0,str.length-1)
                //             return `
				// 			<span>${str}</span>
				// 			`;
                //         }else{
                //             return '-';
                //         }
                //     }
                // },
                {
                    field : 'auditStatus',
                    title : '审核状态',
                    align : 'center',
                    templet : function( d ) {
                        if (d.auditStatus == 1) {
                            return '<span class="label label-primary">已审核</span>';
                        } else if (d.auditStatus == 0) {
                            return '<span class="label label-warning">待审核</span>';
                        }
                    }
                },
                // {
                //     field : 'auditResult',
                //     title : '审核结果',
                //     width:100,
                //     align : 'center',
                //     templet : function( d ) {
                //         if (d.auditResult === "0") {
                //             return '<span class="label label-primary">通过</span>';
                //         }else if (d.auditResult === "1") {
                //             return '<span class="label label-warning">拒绝</span>';
                //         }else if (d.auditResult === "2") {
                //             return '<span class="label label-primary">未审核</span>';
                //         }
                //     }
                // },
                {
                    field : 'createTime',
                    title : '创建时间',
                    align : 'center',
                    templet:function (d) {
                        return `<span>${d.createTime}</span>`
                    }
                },
                {
                    title : '操作',
                    field : 'id',
                    align : 'center',
                    width:160,
                    templet : function(d) {
                        var e = '<a  class="btn btn-primary btn-sm '+s_edit_h+' " href="#" mce_href="#" title="详情" onclick="lookInfo(\''
                            + d.id
                            + '\')"><i class="fa fa-send"></i></a> ';

                        var b = '<a  class="btn btn-primary btn-sm '+s_edit_h+' " href="#" mce_href="#" title="编辑" onclick="edit(\''
                            + d.id + '\')"><i class="fa fa-edit"></i></a> ';

                        var ss = '<a class="btn btn-success btn-sm '+s_remove_h+' " href="#" title="删除"  mce_href="#" onclick="remove(\''
                            + d.id
                            + '\')"><i class="fa fa-remove"></i></a>';
                        return e + (d.auditResult==1||d.status==2?'':b) + (d.status==2||d.auditStatus==0||d.auditResult==1?ss:'');
                    }
                }
            ]]
            ,response: {
                statusCode: 0 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                if( res.code != 0 ){

                    return false;
                }
                if(Number(res.data.total)>0&&res.data.records.length==0){
                    pages--;
                    if(pages>0) {
                        table.reload('exampleTable', {
                            page: {curr: pages},
                            where: {
                                username: $("#username").val(),
                                auditStatus: parseInt($("#auditStatus").val()),
                                mobile:$("#phone").val(),
                                status:$('#myStatus').val(),
                                // projectId: $("#mySelect-projectId").val(),
                                communityName: $("#communityName").val(),
                                movieHouseTime: $("#createTime").val(),
                                movieHouseTime1: $("#endTime").val(),
                                leaveTime: $("#createTime1").val(),
                                leaveTime1: $("#endTime1").val(),
                            }
                        })
                    }
                }

                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            ,jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    pages = obj.curr
                }
            }
            ,error: function(res, msg){

            }

            ,done: function (res){
                form.render()
                hindLoading()
            }
        });

        var $ = layui.$, active = {
            reload: function(){
                //执行重载
                table.reload('exampleTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        username:$("#username").val(),
                        auditStatus:parseInt($("#auditStatus").val()),
                        mobile:$("#phone").val(),
                        status:$('#myStatus').val(),
                        // projectId: $("#mySelect-projectId").val(),
                        communityName: $("#communityName").val(),
                        movieHouseTime: $("#createTime").val(),
                        movieHouseTime1: $("#endTime").val(),
                        leaveTime: $("#createTime1").val(),
                        leaveTime1: $("#endTime1").val(),
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //触发表格复选框选择
        table.on('checkbox(test)', function(obj){
            console.log(obj)
        });

        //触发表格单选框选择
        table.on('radio(test)', function(obj){
            console.log(obj)
        });

    });

});

var pages = 1;
/**
 * 所有的重置表格
 */
function load(){
    layui.use(['table'], ()=>{
        layui.table.reload('exampleTable',{
            where: {
                username:$("#username").val(),
                auditStatus:parseInt($("#auditStatus").val()),
                mobile:$("#phone").val(),
                status:$('#myStatus').val(),
                // projectId: $("#mySelect-projectId").val(),
                communityName: $("#communityName").val(),
                movieHouseTime: $("#createTime").val(),
                movieHouseTime1: $("#endTime").val(),
                leaveTime: $("#createTime1").val(),
                leaveTime1: $("#endTime1").val(),
            }
        })
    })
}

const edit=(id)=>{
    let toIndex=layer.open({
        type: 2,
        title:'修改住宿',
        maxmin: true,
        shadeClose: false,
        area:['50%','700px'],
        content: `/userAunt/jazUserAuntStay/add/edit/${id}`
    });
    // layer.full(toIndex);
}
$("#add").click(function (){

    let toIndex=layer.open({
        type: 2,
        title:'新增住宿',
        maxmin: true,
        shadeClose: false,
        area:['50%','700px'],
        content: '/userAunt/jazUserAuntStay/add/add/-1'
    });
    // layer.full(toIndex);
})

function lookInfo(id){
    console.log('idid',id)
    let toIndex= layer.open({
        type: 2,
        title:'住宿详情',
        maxmin: true,
        shadeClose: false,
        area:['800px','500px'],
        content:  `/userAunt/jazUserAuntStay/info/${id}`
    });
    layer.full(toIndex);
}

function remove(id) {

    layer.confirm('确定要删除选中的记录？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            url : '/userHouse/deletedById',
            type : "post",
            data : {
                'id' : id
            },
            success : function(r) {
                if (r.code==0) {
                    layer.msg(r.msg);
                    load();
                }else{
                    layer.msg(r.msg);
                }
            }
        });
    })
}